﻿using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using RpcGenerator.AS3;
using RpcGenerator.CSharp;
using RpcGenerator.Lua;
using MessageSystem.Managed.Rpc;

namespace RpcGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length <= 1)
            {
                Console.WriteLine("RpcGenerator.exe [TargetAssembly] [-protobuf] [true/false] [-as3] [output] [-csharp] [output] [-cpp] [output]");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
            }
            else
            {
                string assemblyPath = args[0];
                MessageFormatType format = MessageFormatType.Binary;
                for (int i = 1; i < args.Length; )
                {
                    string arg = args[i];
                    string value = args[i + 1];
                    i += 2;
                    if (string.Equals(arg, "-cs"))
                    {
                        CSharpGenerate(assemblyPath, value, format);
                    }
                    else if (string.Equals(arg, "-as3"))
                    {
                        AS3Generate(assemblyPath, value);
                    }
                    else if (string.Equals(arg, "-lua"))
                    {
                        LuaGenerate(assemblyPath, value, format);
                    }
                    else if (string.Equals(arg, "-format"))
                    {
                        if (string.Equals(value, "protobuf"))
                        {
                            format = MessageFormatType.Protobuf;
                        }
                        else if (string.Equals(value, "json"))
                        {
                            format = MessageFormatType.Json;
                        }
                        else
                        {
                            format = MessageFormatType.Binary;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error Input lang={0} output={1}", arg, value);
                    }
                }
            }
            Console.ReadLine();
        }

        static void AS3Generate(string assemblyFile, string output)
        {
            Assembly assembly = Assembly.LoadFrom(assemblyFile);
            if (null != assembly)
            {
                assembly.GetTypes().ToList().ForEach(type =>
                {
                    if (type.GetCustomAttributes(typeof(RpcAttribute), false).Length > 0)
                    {
                        AS3RpcGenerator g = new AS3RpcGenerator();
                        g.Generator(type, output);
                        Console.WriteLine("{0} {1}", type.Name, output);
                    }
                });
            }
        }

        static void CSharpGenerate(string assemblyFile, string output, MessageFormatType format)
        {
            Assembly assembly = Assembly.LoadFrom(assemblyFile);
            if (null != assembly)
            {
                assembly.GetTypes().ToList().ForEach(type =>
                {
                    if (type.GetCustomAttributes(typeof(RpcAttribute), false).Length > 0)
                    {
                        RpcGenerator g = new RpcGenerator();
                        g.Generator(type, output, format);
                        Console.WriteLine("{0} {1}", type.Name, output);
                    }
                });
                RpcGenerator.GenerateShareField(output, format);
            }
        }

        static void LuaGenerate(string assemblyFile, string output, MessageFormatType format)
        {
            Assembly assembly = Assembly.LoadFrom(assemblyFile);
            if (null != assembly)
            {
                assembly.GetTypes().ToList().ForEach(type =>
                {
                    if (type.GetCustomAttributes(typeof(RpcAttribute), false).Length > 0)
                    {
                        LuaRpcGenerator g = new LuaRpcGenerator();
                        g.Generator(type, output, format);
                    }
                });
            }
        }
    }
}
